﻿using System;
using Mono.Collections.Generic;

namespace Mono.Cecil
{
	// Token: 0x02000092 RID: 146
	internal sealed class MemberDefinitionCollection<T> : Collection<T> where T : IMemberDefinition
	{
		// Token: 0x06000559 RID: 1369 RVA: 0x00003F22 File Offset: 0x00002122
		internal MemberDefinitionCollection(TypeDefinition container)
		{
			this.container = container;
		}

		// Token: 0x0600055A RID: 1370 RVA: 0x00003F33 File Offset: 0x00002133
		internal MemberDefinitionCollection(TypeDefinition container, int capacity) : base(capacity)
		{
			this.container = container;
		}

		// Token: 0x0600055B RID: 1371 RVA: 0x00003F45 File Offset: 0x00002145
		protected override void OnAdd(T item, int index)
		{
			this.Attach(item);
		}

		// Token: 0x0600055C RID: 1372 RVA: 0x00003F45 File Offset: 0x00002145
		protected sealed override void OnSet(T item, int index)
		{
			this.Attach(item);
		}

		// Token: 0x0600055D RID: 1373 RVA: 0x00003F45 File Offset: 0x00002145
		protected sealed override void OnInsert(T item, int index)
		{
			this.Attach(item);
		}

		// Token: 0x0600055E RID: 1374 RVA: 0x00003F50 File Offset: 0x00002150
		protected sealed override void OnRemove(T item, int index)
		{
			MemberDefinitionCollection<T>.Detach(item);
		}

		// Token: 0x0600055F RID: 1375 RVA: 0x0001E6B0 File Offset: 0x0001C8B0
		protected sealed override void OnClear()
		{
			foreach (T element in this)
			{
				MemberDefinitionCollection<T>.Detach(element);
			}
		}

		// Token: 0x06000560 RID: 1376 RVA: 0x0001E704 File Offset: 0x0001C904
		private void Attach(T element)
		{
			bool flag = element.DeclaringType == this.container;
			if (!flag)
			{
				bool flag2 = element.DeclaringType != null;
				if (flag2)
				{
					throw new ArgumentException("Member already attached");
				}
				element.DeclaringType = this.container;
			}
		}

		// Token: 0x06000561 RID: 1377 RVA: 0x00003F5A File Offset: 0x0000215A
		private static void Detach(T element)
		{
			element.DeclaringType = null;
		}

		// Token: 0x0400015C RID: 348
		private TypeDefinition container;
	}
}
